package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.ProjectBizService;
import com.neusoft.weekreport.component.entities.DcProject;
import com.neusoft.weekreport.component.entities.DcProjectExample;
import com.neusoft.weekreport.component.req.GetAddProjectInfoReq;
import com.neusoft.weekreport.component.req.GetProjectInfoListReq;
import com.neusoft.weekreport.component.resp.GetProjectInfoByIdResp;
import com.neusoft.weekreport.component.resp.GetProjectInfoListResp;
import com.neusoft.weekreport.mapper.DcProjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProjectBizServiceImpl  implements ProjectBizService {

    @Resource
    DcProjectMapper dcProjectMapper;

    //查询项目列表
    @Override
    public GetProjectInfoListResp getProjectInfoList(GetProjectInfoListReq req) throws Exception {
        GetProjectInfoListResp resp = new GetProjectInfoListResp();
        DcProjectExample dcProjectExample = new DcProjectExample();
        DcProjectExample.Criteria criteria = dcProjectExample.createCriteria();
        if(!StringUtils.isEmpty(req.getProjectName())){
            criteria.andProjectNameLike("%"+req.getProjectName()+"%");
        }
        if(req.getStateValue().equals(1)){
            criteria.andEnableEqualTo(1);
        }
        if(req.getStateValue().equals(0)){
            criteria.andEnableEqualTo(0);
        }
        dcProjectExample.setOrderByClause("order_Num");
        List<DcProject> dcProjectList = dcProjectMapper.selectByExample(dcProjectExample);
        resp.setDcProjectList(dcProjectList);
        return resp;
    }

    //添加项目
    @Override
    public void addProjectInfo(GetAddProjectInfoReq req) throws Exception {
        DcProject dcProject = new DcProject();
        dcProject.setId(UUID.randomUUID().toString());
        dcProject.setProjectName(req.getProjectName());
        dcProject.setSummary(req.getSummary());
        dcProject.setEnable(1);
        dcProject.setCreateDate(sysDateTime());
        dcProject.setUpdateDate(sysDateTime());
        dcProjectMapper.insert(dcProject);
    }

    //修改项目
    @Override
    public  void modifyProjectInfoById(GetAddProjectInfoReq req) throws Exception{
        DcProject dcProject = new DcProject();
        dcProject.setId(req.getId());
        dcProject.setProjectName(req.getProjectName());
        dcProject.setSummary(req.getSummary());
        dcProject.setUpdateDate(sysDateTime());
        dcProjectMapper.updateByPrimaryKeySelective(dcProject);
    }
    //删除项目
    @Override
    public void deleteProjectById(GetAddProjectInfoReq req) throws Exception{
        dcProjectMapper.deleteByPrimaryKey(req.getId());
    }
    //查看项目
    @Override
    public GetProjectInfoByIdResp getProjectInfoById(GetAddProjectInfoReq req) throws Exception{
        GetProjectInfoByIdResp resp = new GetProjectInfoByIdResp();
        DcProject dcProject = dcProjectMapper.selectByPrimaryKey(req.getId());
        resp.setDcProject(dcProject);
        return resp;
    }
    //改变项目状态
    @Override
    public void changeProjectStatusById(GetAddProjectInfoReq req) throws Exception{
        DcProject dcProject = new DcProject();
        dcProject.setId(req.getId());
        if (req.getStateValue().equals(1)){
            dcProject.setEnable(0);
        }
        if (req.getStateValue().equals(0)){
            dcProject.setEnable(1);
        }
        dcProject.setUpdateDate(sysDateTime());
        dcProjectMapper.updateByPrimaryKeySelective(dcProject);
    }

    private String sysDateTime(){
        Date date = new Date();
        DateFormat dataFormat = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
        String createDate = dataFormat.format(date);
        return createDate;
    }

    //查询有效项目
    @Override
    public GetProjectInfoListResp getValidProjectInfoList(GetProjectInfoListReq req) throws Exception {
        GetProjectInfoListResp resp = new GetProjectInfoListResp();
        DcProjectExample dcProjectExample = new DcProjectExample();
        DcProjectExample.Criteria criteria = dcProjectExample.createCriteria();
        criteria.andEnableEqualTo(1);
        dcProjectExample.setOrderByClause("order_Num");
        List<DcProject> dcProjectList = dcProjectMapper.selectByExample(dcProjectExample);
        resp.setDcProjectList(dcProjectList);
        return resp;
    }
}
